package com.house.rent.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.util.MaterialDialogUtils;
import com.house.rent.App;
import com.house.rent.R;
import com.house.rent.entity.HouseEntity;
import com.house.rent.entity.UserEntity;
import com.house.rent.view.XImageView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class HouseDetailActivity extends BaseActivity {

    @BindView(R.id.rl_house_detail_root)
    ViewGroup mRootView;
    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.tv_house_type)
    TextView mTvType;
    @BindView(R.id.tv_house_communityName)
    TextView mTvCommunityName;
    @BindView(R.id.tv_house_floor)
    TextView mTvFloor;
    @BindView(R.id.tv_house_apartment)
    TextView mTvApartment;
    @BindView(R.id.tv_house_area)
    TextView mTvArea;
    @BindView(R.id.tv_house_price)
    TextView mTvPrice;
    @BindView(R.id.tv_house_address)
    TextView mTvAddress;
    @BindView(R.id.tv_house_desc)
    TextView mTvDesc;
    @BindView(R.id.btn_house_collection)
    Button mBtnCollection;
    @BindView(R.id.btn_house_del)
    Button mBtnDel;


    private long mHouseId;
    private HouseEntity mHouseEntity;

    public static void start(Context context, long houseId) {
        Intent starter = new Intent(context, HouseDetailActivity.class);
        starter.putExtra("houseId", houseId);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_house_detail;
    }

    @Override
    protected void initTop() {
        setTitle("房源详情");
        mHouseId = getIntent().getLongExtra("houseId", 0);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mRootView.setVisibility(View.INVISIBLE);
    }

    private void initHouseView() {
        mRootView.setVisibility(View.VISIBLE);
        //如果是自己的房源，可编辑
        if (mHouseEntity.getUserId() == App.getInstance().getUserEntity().getId()) {
            enableRight("编辑", v -> EditHouseActivity.start(mContext, mHouseEntity.getId()));
        }

        initBanner();
        //类型 0-出售 1-求购 2-求租 3-出租
        switch (mHouseEntity.getType()) {
            case 0:
                mTvType.setText("出售");
                break;
            case 1:
                mTvType.setText("求购");
                break;
            case 2:
                mTvType.setText("求租");
                break;
            case 3:
                mTvType.setText("出租");
                break;
            default:
                break;
        }

        mTvCommunityName.setText(mHouseEntity.getCommunityName());
        mTvFloor.setText(mHouseEntity.getFloor() + "楼");
        mTvApartment.setText(mHouseEntity.getApartment());
        mTvArea.setText(String.valueOf(mHouseEntity.getSize()));
        mTvAddress.setText(mHouseEntity.getAddress());
        mTvPrice.setText(String.valueOf(mHouseEntity.getCeilingPrice()));
        mTvDesc.setText(mHouseEntity.getDescription());

        UserEntity userEntity = App.getInstance().getUserEntity();

        //收藏按钮:自己发布的房子，无需收藏
//        if (mHouseEntity.getUserId() == userEntity.getId()) {
//            mBtnCollection.setVisibility(View.GONE);
//        } else {
//            mBtnDel.setVisibility(View.VISIBLE);
//        }

        //删除按钮
        if (App.getInstance().getUserEntity().isAdmin()) {
            mBtnDel.setVisibility(View.VISIBLE);
        } else {
            mBtnDel.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListeners() {

    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {
        addParams("id", mHouseId);
        sendRequest("house-info/info", HouseEntity.class);
    }


    private void initBanner() {
        //设置banner样式
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mBanner.setImageLoader(new ImageLoaderInterface() {

            @Override
            public void displayImage(Context context, Object path, View view) {
                XImageView image = view.findViewById(R.id.iv_house_img);
                image.setImageUrl("data:mime/type;base64," + path);

            }

            @Override
            public View createImageView(Context context) {
                return View.inflate(mContext, R.layout.item_house_img, null);
            }
        });
        // 设置图片集合
        String housePhoto = mHouseEntity.getHousePhoto();
        String[] split = housePhoto.split(",");

        mBanner.setImages(Arrays.asList(split));
        //设置banner动画效果
        mBanner.setBannerAnimation(Transformer.Default);
        //设置标题集合（当banner样式有显示title时）
        //banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        mBanner.isAutoPlay(true);
        mBanner.setOnBannerListener(position ->
        {
        });
        //设置轮播时间
        mBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mBanner.start();
    }

    @OnClick({R.id.btn_house_collection, R.id.btn_house_del})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_house_collection:
                doCollectionReq();
                break;
            case R.id.btn_house_del:
                MaterialDialogUtils.show(mContext, "是否删除房源？",
                        "是", (dialog, which) -> doDelReq());
                break;
            default:
                break;
        }
    }

    /**
     * 请求收藏
     */
    private void doCollectionReq() {
        addParams("houseInfoId", mHouseEntity.getId());
        addParams("userId", App.getInstance().getUserEntity().getId());
        sendRequest("user-collect/collect", null);
    }

    /**
     * 请求删除
     */
    private void doDelReq() {
        addParams("houseInfoId", mHouseEntity.getId());
        addParams("userId", App.getInstance().getUserEntity().getId());
        sendRequest("house-info/delete", null);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        switch (reqId) {
            case "house-info/info":
                if (obj instanceof HouseEntity) {
                    mHouseEntity = (HouseEntity) obj;
                    initHouseView();
                }
                break;
            case "user-collect/collect":
                showMessage("收藏成功");
                finish();
                break;
            case "user-collect/delete":
                showMessage("删除成功");
                finish();
                break;
            default:
                break;
        }
    }

}
